﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Assist
{
	public class StringUtil
	{
		static StringUtil()
		{
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("A", 45217L, 45252L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("B", 45253L, 45760L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("C", 45761L, 46317L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("D", 46318L, 46825L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("E", 46826L, 47009L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("F", 47010L, 47296L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("G", 47297L, 47613L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("H", 47614L, 48118L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("J", 48119L, 49061L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("K", 49062L, 49323L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("L", 49324L, 49895L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("M", 49896L, 50370L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("N", 50371L, 50613L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("O", 50614L, 50621L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("P", 50622L, 50905L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("Q", 50906L, 51386L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("R", 51387L, 51445L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("S", 51446L, 52217L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("T", 52218L, 52697L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("W", 52698L, 52979L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("X", 52980L, 53640L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("Y", 53689L, 54480L));
			StringUtil.dictionaryLetter.Add(new StringUtil.LetterItem("Z", 54481L, 55289L));
		}

		public static string GetLettersOfChinese(string fInputChinese)
		{
			return StringUtil.GetLettersOfChinese(fInputChinese, false);
		}

		public static string GetLettersOfChinese(string fInputChinese, bool fReutrnEmptyWhenFailure)
		{
			string text = "";
			foreach (char c in fInputChinese.ToCharArray())
			{
				text += StringUtil.GetLetterOfPinyin(c.ToString(), fReutrnEmptyWhenFailure);
			}
			return text;
		}

		private static string GetLetterOfPinyin(string fInputSingleChinese, bool fReutrnEmptyWhenFailure)
		{
			byte[] bytes = Encoding.Default.GetBytes(fInputSingleChinese);
			if (bytes.Length != 1)
			{
				short num = (short)bytes[0];
				short num2 = (short)bytes[1];
				long num3 = (long)(num * 256 + num2);
				foreach (StringUtil.LetterItem letterItem in StringUtil.dictionaryLetter)
				{
					if (num3 >= letterItem.MinValue && num3 <= letterItem.MaxValue)
					{
						return letterItem.Letter;
					}
				}
				return "?";
			}
			if (!fReutrnEmptyWhenFailure)
			{
				return string.Empty;
			}
			return fInputSingleChinese.ToUpper();
		}

		public static bool isNumberic1(string _string)
		{
			if (string.IsNullOrEmpty(_string))
			{
				return false;
			}
			foreach (char c in _string)
			{
				if (!char.IsDigit(c))
				{
					return false;
				}
			}
			return true;
		}

		public static bool isNumberic2(string _string)
		{
			if (string.IsNullOrEmpty(_string))
			{
				return false;
			}
			foreach (char c in _string)
			{
				if (!char.IsDigit(c) && c != '.')
				{
					return false;
				}
			}
			return true;
		}

		private static List<StringUtil.LetterItem> dictionaryLetter = new List<StringUtil.LetterItem>();

		private class LetterItem
		{
			public string Letter
			{
				get
				{
					return this.fLetter;
				}
			}

			public long MinValue
			{
				get
				{
					return this.fMinValue;
				}
			}

			public long MaxValue
			{
				get
				{
					return this.fMaxValue;
				}
			}

			public LetterItem(string fLetter, long fMinValue, long fMaxValue)
			{
				this.fLetter = fLetter;
				this.fMinValue = fMinValue;
				this.fMaxValue = fMaxValue;
			}

			private string fLetter;

			private long fMinValue;

			private long fMaxValue;
		}
	}
}
